package com.hw.one.peach.core.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.hw.one.core.controller.BaseController;
import com.hw.one.core.controller.PageController;
import com.hw.one.core.result.MsgResult;
import com.hw.one.peach.core.service.IEncircledAreaService;
import com.hw.one.peach.core.service.IMapPointService;

/**
  * @author  sjd 
  * @date 创建时间：2017年7月29日 下午6:47:51 
  * @version 1.0.0
  */
@RestController
@RequestMapping(path = "mapPoint")
public class MapPointController extends BaseController {

	@Resource
	private IMapPointService mapPointService;
	
	@Resource
	private IEncircledAreaService encircledAreaService;
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "pointInfos", method = RequestMethod.GET)
	public MsgResult getPointInfos(HttpServletRequest request) {
		Map<String, Object> params = new HashMap<String,Object>();
		List<Map<String,Object>> resultList = new ArrayList<>();
		List<Map<String,Object>> pointList;
		Map<String,Object> map;
		String areaCode;
		String areaName;
		List<Map<String,Object>> areaList = encircledAreaService.findEncircledAreaInfos(params);
		
		
		try {
			
			if(areaList!=null&&areaList.size()>0){
				for(int i=0;i<areaList.size();i++){
					map = new HashMap<>();
					areaCode = (String)areaList.get(i).get("areaCode");
					areaName = (String)areaList.get(i).get("areaName");
					map.put("areaCode", areaCode);
					pointList = mapPointService.retrievePointInfos(map);
					map.remove("areaCode");
					if(pointList!=null && pointList.size()>0){
						map.put(areaCode, pointList);
						resultList.add(map);
					}
					
				}
			}
			return MsgResult.okResult(resultList);
		}catch (Exception e) {
			log.error("查询站点信息", e);
			return MsgResult.exceptionResult();
		}

	}
	
	
	
	
	
	
	@Override
	public MsgResult add(HttpServletRequest request){
		Map<String,Object> params = wrapParams(request);
		
			try{
				return mapPointService.add(params);
			}catch(Exception e){
				log.error("add EncircledArea fail"+e.getMessage());
				return MsgResult.exceptionResult();
			}
		
		
	}
	
	@Override
	public MsgResult edit(HttpServletRequest request, @PathVariable String id) {
		Map<String, Object> params = wrapParams(request);
		params.put("id", id);

		try {
			// 需要验证时添加
			return mapPointService.edit(params);
		} catch (Exception e) {
			log.error("账户修改", e);
			return MsgResult.exceptionResult();
		}
	}
	
	
	@Override
	public MsgResult getById(HttpServletRequest request, @PathVariable String id) {
		try {
			return mapPointService.getById(id);
		} catch (Exception e) {
			log.error("查询用户", e);
			return MsgResult.exceptionResult();
		}
	}
	
	 @Override
	public MsgResult delete(HttpServletRequest request, @PathVariable String id) {
			try {
				return mapPointService.delete(id);
			} catch (Exception e) {
				log.error("账户客户端失败", e);
				return MsgResult.exceptionResult();
			}
		}
	
	 
	 @RequestMapping(value = "/deletes", method = RequestMethod.POST)
	public MsgResult deletes(HttpServletRequest request) {
			Map<String, Object> params = wrapParams(request);
			try {
				return mapPointService.deletes(params);
			} catch (Exception e) {
				log.error("批量删除客户端信息", e);
				return MsgResult.exceptionResult();
			}
		}

}
